# VideoScreen - This screen allows user to browse video library content
# Copyright (C) 2007 David Stanczak
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, David Stanczak"
__author__ = "David Stanczak <david.stanczak@gmail.com>"

import clutter
import gtk
import os
    
class Desktop(clutter.Texture):
    
    def __init__(self):
        """
        Intialize desktop widget. Takes a screenshot of current desktop.
        """
        clutter.Texture.__init__(self)
        seconds = 0.8
        self.callback = None
        
        # get screenshot of the desktop
        rwindow = gtk.gdk.get_default_root_window()
        (rw, rh) = rwindow.get_size()
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,rw,rh)
        pixbuf = pixbuf.get_from_drawable(rwindow,rwindow.get_colormap(),0,0,0,0,rw,rh)
        self.set_pixbuf(pixbuf)
        
        self.shown = True
        self.timeline = clutter.Timeline(68,85)
        self.timeline.set_loop(False)
        
    def fade_out(self, callback_function = None):
        """
        Fade out desktop. Desktop spins away from screen. This animation is
        displayed when Entertainer is started up.
        @param callback_function: Call after animation is completed
        """
        self.alpha = clutter.Alpha(self.timeline, clutter.smoothstep_inc_func)
        self.behaviour = clutter.BehaviourOpacity(self.alpha, 254,0)
        self.behaviour.apply(self)
        
        self.behaviour2 = clutter.BehaviourDepth(self.alpha, 0,-5000)
        self.behaviour2.apply(self)
        
        self.behaviour3 = clutter.BehaviourRotate(self.alpha, clutter.X_AXIS, clutter.ROTATE_CW, 0, 45)
        (w,h)=self.get_size()
        self.behaviour3.set_center(w/2,h,0)
        self.behaviour3.apply(self)
        
        if(callback_function is not None):
            self.callback = self.timeline.connect('completed', callback_function)
        else:
            self.timeline.disconnect(self.callback)
            
        self.timeline.rewind()
        self.timeline.start()
        self.shown = False
        
    def fade_in(self, callback_function = None):
        """
        Fade in desktop. Desktop spins back to the screen. This animation is
        displayed when Entertainer is shuting down.
        @param callback_function: Call after animation is completed
        """
        self.alpha = clutter.Alpha(self.timeline, clutter.smoothstep_dec_func)
        self.behaviour = clutter.BehaviourOpacity(self.alpha, 254, 0)
        self.behaviour.apply(self)
        
        self.behaviour2 = clutter.BehaviourDepth(self.alpha, 0, -5000)
        self.behaviour2.apply(self)
        
        self.behaviour3 = clutter.BehaviourRotate(self.alpha, clutter.X_AXIS, clutter.ROTATE_CW, 0, 45)
        (w,h)=self.get_size()
        self.behaviour3.set_center(w/2,h,0)
        self.behaviour3.apply(self)
        
        if(callback_function is not None):
            self.callback = self.timeline.connect('completed', callback_function)
        else:
            self.timeline.disconnect(self.callback)
        
        self.timeline.rewind()
        self.timeline.start()
        self.shown = True
        